package com.guofen.service.shlj.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.pojo.ShljAbnormalGarbage;
import com.guofen.pojo.ShljDeodorizationtowerWaterChange;
import com.guofen.pojo.ShljDeodorizationtowerWaterChange1;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.guofen.service.shlj.EquipmentRunningtimeService;
import com.guofen.dao.ShljEquipmentRunningtimeMapper;
import com.guofen.pojo.ShljEquipmentRunningtime;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EquipmentRunningtimeServiceImpl implements EquipmentRunningtimeService{
    @Autowired
    private ShljEquipmentRunningtimeMapper shljEquipmentRunningtimeMapper;

    @Override
    public boolean addStartupTime(String startupTime) {
        String date = DateAndTimeUtil.getDate();

        ShljEquipmentRunningtime shljEquipmentRunningtime = new ShljEquipmentRunningtime();
        shljEquipmentRunningtime.setDate(date);
        shljEquipmentRunningtime.setStartupTime(startupTime);
        int result = shljEquipmentRunningtimeMapper.insert(shljEquipmentRunningtime);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public boolean addShutdownTime(String shutdownTime, String shutdownReasion) {
        ShljEquipmentRunningtime shljEquipmentRunningtime = shljEquipmentRunningtimeMapper.selectLast();
        System.out.println("最新记录："+ shljEquipmentRunningtime);
        shljEquipmentRunningtime.setShutdownTime(shutdownTime);
        shljEquipmentRunningtime.setShutdownReasion(shutdownReasion);
        int result = shljEquipmentRunningtimeMapper.updateByPrimaryKeySelective(shljEquipmentRunningtime);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<ShljEquipmentRunningtime> list = shljEquipmentRunningtimeMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<ShljEquipmentRunningtime> list = shljEquipmentRunningtimeMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return shljEquipmentRunningtimeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(ShljEquipmentRunningtime record) {
        return shljEquipmentRunningtimeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<ShljEquipmentRunningtime> findForm() {
        return shljEquipmentRunningtimeMapper.findLast();
    }
}